package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yak.admin/src/middleware"
	"yak.admin/src/module/backend/admin/form"
	"yak.admin/src/util"
)

func Login(c *gin.Context) {
	var f form.LoginForm
	if c.ShouldBind(&f) == nil {
		if admin, ok := f.Login(); ok {
			util.SaveSession(c, util.SessionAdminKey, admin)
			c.Redirect(http.StatusFound, "/")
			return
		}
	}
	c.Set("Page", middleware.Page{
		Tpl:    "layui/auth/login.html",
		Data: map[string]interface{}{
			"username": f.Username, "password": "", "error": "登录失败！",
		},
	})
}
